<?php
namespace Home\Controller;

use Think\Controller;
use Think\Model;

class WebChatApiController extends Controller
{
    var $user_id;
    /** 
     * token验证
     */
    function _initialize()
    {
        $data = get_all_headers();
        $this->user_id = '';
        $this->role = '';
        $this->mobile = '';
        if (isset($data['token']) && !empty($data['token'])) {
            $ret = D('User')->getUserInfoByToken($data['token']);
            if ($ret['code'] == 1) {
                $this->user_id = $ret['user_id'];
                $this->name = $ret['name'];
                $this->username = $ret['username'];
                $this->contract = $ret['contract'];
                $this->role = $ret['role'];
                $this->mobile = $ret['mobile'];
            }
        }
    }

    /*
     * Function:getWxLoginCode
     * Description:根据登录code获取微信接口的openid和session_key
     */
    public function getWxLoginCode()
    {
        $return = array();
        $code = I('code');
        $nickName = I('nickName');
        $avatarUrl = I('avatarUrl');
        $gender = I('gender', '');
        $city = I('city');
        $province = I('province');
        $country = I('country');
        $userData = array();
        if (!empty($code)) {
            $appId = C('appid');
            $appsecret = C('appsecret');
            $url = C('codeUrl') . '?appid=' . $appId . '&secret=' . $appsecret . '&js_code=' . $code . '&grant_type=authorization_code';
            $data = curl_get($url);
            $data = json_decode($data, true);
            if ($data['errcode'] == '40029') { //错误的code请求
                $return['code'] = 0;
                $return['errorMsg'] = 'code请求错误';
                $this->ajaxReturn($return);
            } else {
                if (empty($data['openid'])) {
                    $return['code'] = 0;
                    $return['errorMsg'] = '数据异常，请联系客服';
                    $this->ajaxReturn($return);
                }
                $userData['username'] = emojiEncode($nickName);
                $userData['nickname'] = emojiEncode($nickName);
                $userData['avatarurl'] = $avatarUrl;
                $userData['sex'] = !empty($gender) ? $gender : 2;
                $userData['city'] = $city;
                $userData['province'] = $province;
                $userData['country'] = $country;
                $userData['openid'] = $data['openid'];
                $userData['session_key'] = $data['session_key'];
                $userData['unionid'] = $data['unionid'];
                $userData['sessionid'] = md5($data['openid'] . $code);
                $userData['addtime'] = date('Y-m-d H:i:s', time());
                if ($rsInfo = D('User')->getUsersInfoByOpenId($userData['openid'])) {
                    $userData['id'] = $rsInfo['id'];
                    if ($rsInfo['nickname'] != '微信用户') {
                        $userData['username'] = $rsInfo['username'];
                        $userData['nickname'] = $rsInfo['nickname'];
                        $userData['avatarurl'] = $rsInfo['avatarurl'];
                    }
                    D('User')->updateUser($userData);
                } else {
                    $userData['number_no'] = D('User')->generateNumberNo();
                    $userData['id'] = D('User')->addUser($userData);
                    if (!empty(C('is_use_integral'))) {
                        //增加积分记录
                        $integral = [];
                        $integral['type'] = 1;
                        $integral['user_id'] = $userData['id'];
                        $integral['title'] = '新用户注册增加积分';
                        $integral['integral'] = 10;
                        $integral['addtime'] = date('Y-m-d H:i:s', time());
                        M('user_integral_logs')->add($integral);
                        //增加用户积分
                        $uData = array();
                        $uData['integral'] = array('exp', 'integral+' . $integral['integral']);
                        M('user')->where(['id' => $integral['user_id']])->save($uData);
                    }
                }
            }
            $return['code'] = 1;
            $return['errorMsg'] = $userData['sessionid'];
            $return['id'] = $userData['id'];
            $return['userInfo']['nickName'] = $userData['nickname'];
            $return['userInfo']['avatarUrl'] = $userData['avatarurl'];
            $this->ajaxReturn($return);
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = 'code为空';
            $this->ajaxReturn($return);
        }
    }

    /* Function:bindUserMobile
     * Description:绑定用户手机号
     */
    public function bindUserMobile()
    {
        $appId = C('appid');
        $sessionid = I('sessionid', '');
        if (!$rsInfo = M('user')->where(array('sessionid' => $sessionid))->find()) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '不存在此用户']);
        }
        $iv = I('iv', '');
        $encryptedData = I('encryptedData', '');
        if (empty($iv) || empty($encryptedData)) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '参数有误']);
        }
        if (!$info = M('user')->where(array('sessionid' => $sessionid))->find()) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '不存在此用户']);
        }
        $sessionKey = $info['session_key'];
        if (strlen($sessionKey) != 24) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => 'encodingAesKey 非法']);
        }
        $aesKey = base64_decode(str_replace(' ', '+', $sessionKey));
        if (strlen($iv) != 24) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => 'iv 非法']);
        }
        $aesIV = base64_decode(str_replace(' ', '+', $iv));
        $aesCipher = base64_decode(str_replace(' ', '+', $encryptedData));
        $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
        $dataObj = json_decode($result);
        if ($dataObj == NULL) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '请尝试重新授权']);
        }
        if ($dataObj->watermark->appid != $appId) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => 'appid 非法']);
        }
        $retData = json_decode($result, true);
        $mobile = $retData['phoneNumber'];
        $updata = [];
        $updata['mobile'] = $mobile;
        //检测是否会员已经生成
        if ($userInfo = M('user')->where(['mobile' => $mobile, 'status' => 1, 'is_deleted' => 0])->find()) {
            if (empty($userInfo['sessionid'])) {
                $uData = [];
                if (!empty($rsInfo['avatarurl'])) {
                    $uData['avatarurl'] = $rsInfo['avatarurl'];
                }
                $uData['openid'] = $rsInfo['openid'];
                $uData['unionid'] = $rsInfo['unionid'];
                $uData['session_key'] = $rsInfo['session_key'];
                $uData['sessionid'] = $rsInfo['sessionid'];
                $ret = M('user')->where(['id' => $userInfo['id']])->save($uData);
                if ($ret !== false) {
                    M('user')->where(['id' => $rsInfo['id']])->delete();
                    $this->ajaxReturn(['code' => 1, 'errorMsg' => 'success', 'mobile' => $mobile, 'role' => $userInfo['role']]);
                } else {
                    $this->ajaxReturn(['code' => 0, 'errorMsg' => '用户手机号绑定失败']);
                }
            } else {
                $ret = M('user')->where(['id' => $userInfo['id']])->save($updata);
                if ($ret !== false) {
                    $this->ajaxReturn(['code' => 1, 'errorMsg' => 'success', 'mobile' => $mobile, 'role' => $userInfo['role']]);
                } else {
                    $this->ajaxReturn(['code' => 0, 'errorMsg' => '用户手机号绑定失败']);
                }
            }
        } else {
            $ret = M('user')->where(['id' => $rsInfo['id']])->save($updata);
            if ($ret !== false) {
                $this->ajaxReturn(['code' => 1, 'errorMsg' => 'success', 'mobile' => $mobile, 'role' => 0]);
            } else {
                $this->ajaxReturn(['code' => 0, 'errorMsg' => '用户手机号绑定失败']);
            }
        }
    }

    /* Function:bindUserMobile
     * Description:绑定用户手机号
     */
    public function rbindUserMobile()
    {
        $appId = C('appid');
        $sessionid = I('sessionid', '');
        if (!$rsInfo = M('user')->where(array('sessionid' => $sessionid))->find()) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '不存在此用户']);
        }
        $code = I('code', '');
        if (!empty($code)) {
            $appsecret = C('appsecret');
            $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appId . '&secret=' . $appsecret . '&js_code=' . $code . '&grant_type=authorization_code';
            $data = curl_get($url);
            $data = json_decode($data, true);
            if (!empty($data['errcode']) && $data['errcode'] == '40029') { //错误的code请求
                $return['code'] = 0;
                $return['errorMsg'] = 'code请求错误';
                echo json_encode($return);
            } else {
                $rsInfo['session_key'] = $data['session_key'];
                M('user')->where(['id' => $rsInfo['id']])->save(['session_key' => $data['session_key']]);
            }
        }
        $iv = I('iv', '');
        $encryptedData = I('encryptedData', '');
        if (empty($iv) || empty($encryptedData)) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '参数有误']);
        }
        $sessionKey = $rsInfo['session_key'];
        if (strlen($sessionKey) != 24) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => 'encodingAesKey 非法']);
        }
        $aesKey = base64_decode(str_replace(' ', '+', $sessionKey));
        if (strlen($iv) != 24) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => 'iv 非法']);
        }
        $aesIV = base64_decode(str_replace(' ', '+', $iv));
        $aesCipher = base64_decode(str_replace(' ', '+', $encryptedData));
        $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
        $dataObj = json_decode($result);
        if ($dataObj == NULL) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '请尝试重新授权']);
        }
        if ($dataObj->watermark->appid != $appId) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => 'appid 非法']);
        }
        $retData = json_decode($result, true);
        $mobile = $retData['phoneNumber'];
        $updata = [];
        $updata['mobile'] = $mobile;
        //检测是否会员已经生成
        if ($userInfo = M('user')->where(['mobile' => $mobile, 'status' => 1, 'is_deleted' => 0])->find()) {
            if (empty($userInfo['sessionid'])) {
                $uData = [];
                if (!empty($rsInfo['avatarurl'])) {
                    $uData['avatarurl'] = $rsInfo['avatarurl'];
                }
                $uData['openid'] = $rsInfo['openid'];
                $uData['unionid'] = $rsInfo['unionid'];
                $uData['session_key'] = $rsInfo['session_key'];
                $uData['sessionid'] = $rsInfo['sessionid'];
                $ret = M('user')->where(['id' => $userInfo['id']])->save($uData);
                if ($ret !== false) {
                    M('user')->where(['id' => $rsInfo['id']])->delete();
                    $this->ajaxReturn(['code' => 1, 'errorMsg' => 'success', 'mobile' => $mobile, 'role' => $userInfo['role']]);
                } else {
                    $this->ajaxReturn(['code' => 0, 'errorMsg' => '用户手机号绑定失败']);
                }
            } else {
                $ret = M('user')->where(['id' => $userInfo['id']])->save($updata);
                if ($ret !== false) {
                    $this->ajaxReturn(['code' => 1, 'errorMsg' => 'success', 'mobile' => $mobile, 'role' => $userInfo['role']]);
                } else {
                    $this->ajaxReturn(['code' => 0, 'errorMsg' => '用户手机号绑定失败']);
                }
            }
        } else {
            $ret = M('user')->where(['id' => $rsInfo['id']])->save($updata);
            if ($ret !== false) {
                $this->ajaxReturn(['code' => 1, 'errorMsg' => 'success', 'mobile' => $mobile, 'role' => 0]);
            } else {
                $this->ajaxReturn(['code' => 0, 'errorMsg' => '用户手机号绑定失败']);
            }
        }
    }

    /* Function:getWxUserInfo
     * Description:获取用户信息
     */
    public function getWxUserInfo()
    {
        $return = array();
        if ($rsInfo = D('User')->getUserInfo($this->user_id)) {
            $rsInfo['username'] = emojiDecode($rsInfo['username']);
            $rsInfo['nickname'] = emojiDecode($rsInfo['nickname']);
            $return['code'] = 1;
            $return['errorMsg'] = $rsInfo;
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '获取用户信息失败';
        }
        $this->ajaxReturn($return);
    }

    /* 
     * Function:getUserInfo
     * Description:获取用户信息
     */
    public function getUserInfo()
    {
        $return = array();
        if ($rsInfo = D('User')->getUserInfo($this->user_id)) {
            $rsInfo['username'] = emojiDecode($rsInfo['username']);
            $return['code'] = 1;
            $return['errorMsg'] = $rsInfo;
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '获取用户信息失败';
        }
        $this->ajaxReturn($return);
    }

    /* 
     * Function:getWxUserInfoByNumberNo
     * Description:获取用户信息 by number_no 会员编号
     */
    public function getWxUserInfoByNumberNo()
    {
        $return = array();
        $numberNo = I('number_no', '');
        if ($rsInfo = D('User')->getUsersInfoByNumberNo($numberNo)) {
            $return['code'] = 1;
            $return['errorMsg'] = $rsInfo['sessionid'];
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '获取用户信息失败';
        }
        $this->ajaxReturn($return);
    }

    /* Function:updateWxUserInfo
     * Description:微信用户信息更新
     */
    public function updateWxUserInfo()
    {
        $return = array();
        $data = array();
        $nickName = I('nickName');
        $avatarUrl = I('avatarUrl');
        $gender = I('gender');
        if ($rsInfo = D('User')->getUserInfo($this->user_id)) {
            $data['id'] = $rsInfo['id'];
            $data['username'] = $nickName;
            $data['nickname'] = $nickName;
            $data['avatarurl'] = $avatarUrl;
            $data['sex'] = $gender;
            $ret = D('User')->updateUser($data);
            if ($ret !== false) {
                $return['code'] = 1;
            } else {
                $return['code'] = 0;
                $return['errorMsg'] = '更新失败';
            }
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '获取用户信息失败';
        }
        $this->ajaxReturn($return);
    }

    /* 
     * Function:addFeedback
     * Description:增加用户反馈
     */
    public function addFeedback()
    {
        $return = array();
        $data = array();
        $type = I('type');
        $content = I('content');
        $sessionid = I('sessionid');
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '用户不存在或者未登录';
        }
        if (empty($content)) {
            $return['code'] = 0;
            $return['errorMsg'] = '反馈内容不能为空';
        }
        $data['uid'] = $rsInfo['id'];
        $data['title'] = $rsInfo['name'] . '(' . $rsInfo['mobile'] . ')的反馈';
        $data['content'] = $content;
        $data['type'] = ($type + 1);
        $data['addtime'] = time();
        if (D('Feedback')->addFeedback($data)) {
            $return['code'] = 1;
            $return['errorMsg'] = 'success';
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '添加失败';
        }
        $this->ajaxReturn($return);
    }

    /* Function:getFeedbackList
     * Description:获取反馈列表
     */
    public function getFeedbackList()
    {
        $return = array();
        $rsInfo = array();
        if ($rsInfo = D('User')->getUserInfo($this->user_id)) {
            if ($feedbackList = D('Feedback')->getFeedbackListAll($rsInfo['id'])) {
                $return['code'] = 1;
                $return['errorMsg'] = $feedbackList;
            } else {
                $return['code'] = 0;
                $return['errorMsg'] = '暂无记录';
            }
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '用户不存在';
        }
        $this->ajaxReturn($return);
    }

    /* Function:getFeedbackInfo
     * Description:获取反馈详细内容
     */
    public function getFeedbackInfo()
    {
        if (empty($this->user_id)) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '用户不存在']);
        }
        $return = [];
        $id = I('id');
        if ($feedbackInfo = D('Feedback')->getFeedbackInfo($id)) {
            $return['code'] = 1;
            $return['errorMsg'] = $feedbackInfo['content'];
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '暂无记录';
        }
        $this->ajaxReturn($return);
    }

    /**
     * 【用户】省市区联动
     * @return:省市区联动
     */
    public function ajaxRegion()
    {
        $return = array();
        $type = I('type', 'province');
        $regionId = I('region_id', '');
        if ($type == 'province') {
            $provinceList = M('regions')->where(['region_grade' => 1, 'status' => 1])->select();
            //默认北京
            if (empty($regionId)) {
                $cityList = M('regions')->where(['region_grade' => 2, 'status' => 1, 'p_region_id' => 1])->select();
                $areaList = M('regions')->where(['region_grade' => 3, 'status' => 1, 'p_region_id' => 2])->select();
            } else {
                $cityList = M('regions')->where(['region_grade' => 2, 'status' => 1, 'p_region_id' => $regionId])->select();
                $areaList = M('regions')->where(['region_grade' => 3, 'status' => 1, 'p_region_id' => $cityList[0]['region_id']])->select();
            }
        } else if ($type == 'city') {
            $areaList = M('regions')->where(['region_grade' => 3, 'status' => 1, 'p_region_id' => $regionId])->select();
        }
        $return['code'] = 1;
        $return['province'] = $provinceList;
        $return['city'] = $cityList;
        $return['area'] = $areaList;
        $this->ajaxReturn($return);
    }

    /* Function:getProvinceList
     * Description:获取省份列表
     */
    public function getProvinceList()
    {
        $return = [];
        $provinceData = [];
        $provinceList = M('regions')->where(['region_grade' => 1, 'status' => 1])->select();
        if (is_array($provinceList) && count($provinceList) > 0) {
            foreach ($provinceList as $key => $val) {
                $provinceData[] = $val['local_name'];
            }
        }
        $return['code'] = 1;
        $return['errorMsg'] = $provinceData;
        $this->ajaxReturn($return);
    }

    /**
     * 【用户收货地址】收货地址列表
     * @return:获取用户收货地址列表
     */
    public function getUserAddressList()
    {
        $return = array();
        $rsInfo = array();
        if ($rsInfo = D('User')->getUserInfo($this->user_id)) {
            if ($addressList = D('UserAddr')->getUserAddrListAll($rsInfo['id'])) {
                $return['code'] = 1;
                $return['errorMsg'] = $addressList;
            } else {
                $return['code'] = 0;
                $return['errorMsg'] = '暂无记录';
            }
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '用户不存在';
        }
        $this->ajaxReturn($return);
    }

    /**
     * 【用户收货地址】增加收货地址
     * @return:array 是否增加成功
     */
    public function addUserAddress()
    {
        $return = array();
        $data = array();
        $ship_name = I('ship_name');
        $phone = I('phone');
        $province_id = I('province_id'); //省id
        $province = I('province'); //省
        $city_id = I('city_id'); //市id
        $city = I('city'); //市
        $area_id = I('area_id'); //区id
        $area = I('area'); //区
        $address = I('address');
        $is_default = I('is_default', '');
        $id = I('id', '');
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '用户不存在或者未登录';
            $this->ajaxReturn($return);
        }
        if (empty($ship_name)) {
            $return['code'] = 0;
            $return['errorMsg'] = '联系人不能为空';
            $this->ajaxReturn($return);
        }
        if (empty($phone)) {
            $return['code'] = 0;
            $return['errorMsg'] = '联系人手机不能为空';
            $this->ajaxReturn($return);
        } else {
            if (!verificationData($phone, 'checkmobile', '')) {
                $return['code'] = 0;
                $return['errorMsg'] = '手机格式有误';
                $this->ajaxReturn($return);
            }
        }
        if (empty($province) || empty($province_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '请选择省份';
            $this->ajaxReturn($return);
        }
        if (empty($city) || empty($city_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '请选择市';
            $this->ajaxReturn($return);
        }
        if (empty($area) || empty($area_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '请选择区';
            $this->ajaxReturn($return);
        }
        if (empty($address)) {
            $return['code'] = 0;
            $return['errorMsg'] = '地址不能为空';
            $this->ajaxReturn($return);
        }
        if ($is_default == 'undefined' || empty($is_default)) {
            $is_default = 0;
        } else {
            $is_default = 1;
        }
        if (!empty($is_default)) {
            M('user_addr')->where(['user_id' => $rsInfo['id']])->save(['is_default' => 0]);
        }
        $data['user_id'] = $rsInfo['id'];
        $data['ship_name'] = $ship_name;
        $data['phone'] = $phone;
        $data['province_id'] = $province_id;
        $data['province'] = $province;
        $data['city_id'] = $city_id;
        $data['city'] = $city;
        $data['area_id'] = $area_id;
        $data['area'] = $area;
        $data['address'] = $address;
        $data['is_default'] = $is_default;
        $data['addtime'] = date('Y-m-d H:i:s', time());
        if (!empty($id)) {
            $data['id'] = $id;
            $ret = D('UserAddr')->updateUserAddr($data);
        } else {
            $ret = D('UserAddr')->addUserAddr($data);
            $ret = !empty($ret) ? true : false;
        }
        if ($ret !== false) {
            $return['code'] = 1;
            $return['errorMsg'] = 'success';
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '添加失败';
        }
        $this->ajaxReturn($return);
    }

    /**
     * 【用户收货地址】获取收货地址详细
     * @return:array 收货地址详细
     */
    public function getUserAddressInfo()
    {
        if (empty($this->user_id)) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '用户不存在']);
        }
        $return = array();
        $id = I('id');
        if ($addressInfo = D('UserAddr')->getUserAddrInfo($id)) {
            $provinceList = M('regions')->where(['region_grade' => 1, 'status' => 1])->select();
            $cityList = M('regions')->where(['region_grade' => 2, 'status' => 1, 'p_region_id' => $addressInfo['province_id']])->select();
            $areaList = M('regions')->where(['region_grade' => 3, 'status' => 1, 'p_region_id' => $addressInfo['city_id']])->select();
            $return['code'] = 1;
            $return['errorMsg'] = $addressInfo;
            $return['province'] = $provinceList;
            $return['city'] = $cityList;
            $return['area'] = $areaList;
            foreach ($provinceList as $key => $val) {
                if ($val['region_id'] == $addressInfo['province_id']) {
                    $provinceKey = $key;
                }
            }
            foreach ($cityList as $key => $val) {
                if ($val['region_id'] == $addressInfo['city_id']) {
                    $cityKey = $key;
                }
            }
            foreach ($areaList as $key => $val) {
                if ($val['region_id'] == $addressInfo['area_id']) {
                    $areaKey = $key;
                }
            }
            $return['provinceKey'] = $provinceKey;
            $return['cityKey'] = $cityKey;
            $return['areaKey'] = $areaKey;
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '暂无记录';
        }
        $this->ajaxReturn($return);
    }

    /**
     * 【用户收货地址】获取收货地址详细
     * @return:array 收货地址详细
     */
    public function getMyAddressInfo()
    {
        $return = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '用户不存在或者未登录';
            $this->ajaxReturn($return);
        }
        $id = I('addr_id', '');
        if (!empty($id)) {
            $addressInfo = M('user_addr')->where(['id' => $id])->find();
        } else {
            $addressInfo = M('user_addr')->where(['user_id' => $rsInfo['id'], 'is_default' => 1])->order('id desc')->limit(1)->find();
        }
        if (!empty($addressInfo)) {
            $return['code'] = 1;
            $return['errorMsg'] = $addressInfo;
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '暂无记录';
        }
        $this->ajaxReturn($return);
    }

    /**
     * 【用户收货地址】删除收货地址
     * @return:array 是否操作成功
     */
    public function delUserAddress()
    {
        if (empty($this->user_id)) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '用户不存在']);
        }
        $return = array();
        $id = I('id');
        if (D('UserAddr')->deleteUserAddr($id)) {
            $return['code'] = 1;
            $return['errorMsg'] = 'success';
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '操作失败';
        }
        $this->ajaxReturn($return);
    }

    /* 
     * Function:getConfigContent
     * Description:获取配置内容
     */
    public function getConfigContent()
    {
        $id = I('id', '');
        if ($info = D('SystemConfig')->getSystemConfigInfo($id)) {
            $this->ajaxReturn(['code' => 1, 'data' => $info]);
        } else {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '获取信息失败']);
        }
    }

    /**
     * 【首页】获取banner图和首页图
     * @return:array
     */
    public function getIndexData()
    {
        $return = array();
        $bannerList = M('system_pic')->where(['type' => 1, 'status' => 1])->order('sort desc,id desc')->select();
        $picList = M('system_pic')->where(['type' => 2, 'status' => 1])->select();
        $storeInfo = M('stores_list')->where(['id' => 1])->find();
        $return['code'] = 1;
        $return['bannerList'] = $bannerList;
        $return['picList'] = $picList;
        $return['storeInfo'] = $storeInfo;
        $this->ajaxReturn($return);
    }

    /**
     * 【商品】获取商品分类列表
     * @return:array
     */
    public function getGoodsCateList()
    {
        $return = [];
        $data = [];
        $goodsCateList = M('goods_cate')->where(['status' => 1, 'pid' => 0])->order('sort desc,id desc')->select();
        $return['code'] = 1;
        $return['errorMsg'] = $goodsCateList;
        $this->ajaxReturn($return);
    }

    /**
     * 【商品】获取商品二级分类
     * @return:array
     */
    public function getGoodsCateSecond()
    {
        $return = [];
        $cateList = [];
        $cate_id = I('cate_id', '');
        if (!empty($cate_id)) {
            $cateList = M('goods_cate')->where(['pid' => $cate_id, 'status' => 1])->order('sort desc,id desc')->select();
        }
        $return['code'] = 1;
        $return['errorMsg'] = $cateList;
        $this->ajaxReturn($return);
    }

    /**
     * 【商品】获取商品列表
     * @return:array
     */
    public function getGoodsList()
    {
        $return = array();
        $type = I('type', 0);
        $is_price_up = I('is_price_up', 0);
        $filter = array();
        $searchname = I('searchname', '');
        $cate = I('cate', '');
        $filter['status'] = 1;
        $filter['is_deleted'] = 0;
        if (!empty($searchname)) {
            $filter['goods_title'] = ['like', '%' . $searchname . '%'];
        }
        if (!empty($cate)) {
            $filter['cate_id'] = $cate;
        }
        $pageNo = I('pageNo', 1);
        $pageSize = I('pageSize', 10);
        if ($type == 0) {
            $goodsList = M('goods')->where($filter)->order('sort desc,id desc')->limit(($pageNo - 1) * $pageSize, $pageSize)->select();
        } else if ($type == 1) { //新品
            $goodsList = M('goods')->where($filter)->order('is_new desc,id desc')->limit(($pageNo - 1) * $pageSize, $pageSize)->select();
        } else if ($type == 2) { //销量
            $goodsList = M('goods')->where($filter)->order('sale_nums desc,id desc')->limit(($pageNo - 1) * $pageSize, $pageSize)->select();
        } else if ($type == 3) { //价格   
            if ($is_price_up == 0) {
                $goodsList = M('goods')->where($filter)->order('goods_price desc,id desc')->limit(($pageNo - 1) * $pageSize, $pageSize)->select();
            } else {
                $goodsList = M('goods')->where($filter)->order('goods_price asc,id desc')->limit(($pageNo - 1) * $pageSize, $pageSize)->select();
            }
        }
        if (is_array($goodsList) && count($goodsList) > 0) {
            foreach ($goodsList as &$val) {
                $val['price'] = M('goods_list')->where(['goods_id' => $val['id'], 'status' => 1])->min('selling_price');
                $val['goods_type'] = "";
                if ($val['is_new'] == 1) {
                    $val['goods_type'] = "NEW";
                } else if ($val['is_hot'] == 1) {
                    $val['goods_type'] = "HOT";
                }
            }
        }
        $return['code'] = 1;
        $return['errorMsg'] = $goodsList;
        $return['loadmore'] = (count($goodsList) == $pageSize) ? true : false;
        $this->ajaxReturn($return);
    }

    /**
     * 【商品】获取商品列表-搜索
     * @return:array
     */
    public function getSearchGoodsList()
    {
        $return = array();
        $filter = array();
        $filter['goods_title'] = I('searchname', '');
        $pageNo = I('pageNo', 1);
        $pageSize = I('pageSize', 10);
        $list = D('Goods')->getGoodsList($pageNo, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as &$val) {
                $val['price'] = M('goods_list')->where(['goods_id' => $val['id'], 'status' => 1])->min('selling_price');
            }
        }
        $return['code'] = 1;
        $return['errorMsg'] = $list;
        $return['loadmore'] = (count($list) == $pageSize) ? true : false;
        $this->ajaxReturn($return);
    }

    /**
     * 【商品】获取商品列表-推荐
     * @return:array
     */
    public function getRecommendGoodsList()
    {
        $return = array();
        $pageNo = I('pageNo', 1);
        $pageSize = I('pageSize', 10);
        $list = M('goods')->where(['status' => 1, 'is_deleted' => 0, 'is_recommend' => 1])->order('sort desc,id desc')->limit(2)->select();
        if (is_array($list) && count($list) > 0) {
            foreach ($list as &$val) {
                $val['goods_type'] = "";
                if ($val['is_new'] == 1) {
                    $val['goods_type'] = "NEW";
                } else if ($val['is_hot'] == 1) {
                    $val['goods_type'] = "HOT";
                }
            }
        }
        $return['code'] = 1;
        $return['errorMsg'] = $list;
        $return['loadmore'] = (count($list) == $pageSize) ? true : false;
        $this->ajaxReturn($return);
    }

    /**
     * 获取新闻分类列表
     * @return:array
     */
    public function getNewsCateList()
    {
        $return = array();
        $filter = array();
        $list = D('NewsType')->getNewsTypeAllList();
        $return['code'] = 1;
        $return['errorMsg'] = $list;
        $this->ajaxReturn($return);
    }

    /**
     * 新闻列表
     * @return:array
     */
    public function getNewsList()
    {
        $return = array();
        $filter = array();
        $pageNo = I('pageNo', 1);
        $pageSize = I('pageSize', 10);
        $cate_id = I('cate_id', 1);
        $filter['tid'] = $cate_id;
        $list = D('News')->getNewsList($pageNo, $filter, $pageSize);
        $return['code'] = 1;
        $return['errorMsg'] = $list;
        $return['loadmore'] = (count($list) == $pageSize) ? true : false;
        $this->ajaxReturn($return);
    }

    /**
     * 新闻详细信息
     * @return:array
     */
    public function getNewsInfo()
    {
        $return = array();
        $filter = array();
        $id = I('id', '');
        $info = M('News')->where(['id' => $id])->find();
        $return['code'] = 1;
        $return['errorMsg'] = $info;
        $this->ajaxReturn($return);
    }

    /**
     * 【用户】获取用户收藏列表
     * @return:array
     */
    public function getUserFavoriteList()
    {
        $return = array();
        $filter = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $filter['user_id'] = $rsInfo['id'];
        $pageNo = I('pageNo', 1);
        $pageSize = I('pageSize', 10);
        $list = D('UserFavorite')->getUserFavoriteList($pageNo, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as &$val) {
                $info = D('Goods')->getGoodsInfo($val['goods_id']);
                if (!empty($val['goods_spec_id'])) {
                    $goodsListInfo = M('goods_list')->where(['goods_id' => $val['goods_id'], 'goods_spec_id' => $val['goods_spec_id']])->find();
                } else {
                    $goodsListInfo = M('goods_list')->where(['goods_id' => $val['goods_id']])->find();
                }
                $val['name'] = $info['goods_title'];
                $val['img'] = empty($goodsListInfo['goods_img_url']) ? $info['goods_small_pic'] : $goodsListInfo['goods_img_url'];
                $val['prize'] = $goodsListInfo['selling_price'];
                $val['size'] = $goodsListInfo['goods_spec'];
            }
        }
        $return['code'] = 1;
        $return['errorMsg'] = $list;
        $return['loadmore'] = (count($list) == $pageSize) ? true : false;
        $this->ajaxReturn($return);
    }

    /**
     * 【用户】收藏动作
     * @return:array
     */
    public function userFavorite()
    {
        $return = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $status = I('status', 1);
        $goods_id = I('goods_id', '');
        $goods_list_id = I('goods_list_id', '');
        $goods_spec_id = I('goods_spec_id', '');
        if (empty($goods_spec_id)) {
            $goodsInfo = M('goods_list')->where(['goods_id' => $goods_id])->find();
            $goods_list_id = $goodsInfo['id'];
            $goods_spec_id = $goodsInfo['goods_spec_id'];
        }
        if ($info = M('user_favorite')->where(['goods_id' => $goods_id, 'goods_spec_id' => $goods_spec_id, 'user_id' => $rsInfo['id']])->find()) {
            if ($info['status'] == 1) {
                $ret = M('user_favorite')->where(['id' => $info['id']])->save(['status' => 0]);
            } else {
                $ret = M('user_favorite')->where(['id' => $info['id']])->save(['status' => 1]);
            }
        } else {
            if (!empty($status)) {
                $data = [];
                $data['type'] = 1;
                $data['user_id'] = $rsInfo['id'];
                $data['goods_id'] = $goods_id;
                $data['goods_list_id'] = $goods_list_id;
                $data['goods_spec_id'] = $goods_spec_id;
                $data['status'] = $status;
                $data['addtime'] = date('Y-m-d H:i:s', time());
                $ret = D('UserFavorite')->addUserFavorite($data);
                $ret = !empty($ret) ? true : false;
            } else {
                $ret = true;
            }
        }
        if ($ret !== false) {
            $return['code'] = 1;
            $return['errorMsg'] = '操作成功';
            $this->ajaxReturn($return);
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '操作失败';
            $this->ajaxReturn($return);
        }
    }

    /**
     * 【用户】收藏删除
     * @return:array
     */
    public function deleteUserFavorite()
    {
        $return = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $id = I('id', '');
        if (empty($id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '缺少必要参数';
            $this->ajaxReturn($return);
        }
        $list = explode(',', $id);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => $val) {
                if (!empty($val)) {
                    M('user_favorite')->where(['id' => $val])->delete();
                }
            }
            $return['code'] = 1;
            $return['errorMsg'] = 'success';
            $this->ajaxReturn($return);
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '操作失败';
            $this->ajaxReturn($return);
        }
    }

    /**
     * 【用户】获取用户订单列表
     * @return:array
     */
    public function getUserOrdersList()
    {
        $return = array();
        $filter = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $filter['user_id'] = $rsInfo['id'];
        $filter['status'] = I('status');
        $pageNo = I('pageNo', 1);
        $pageSize = I('pageSize', 10);
        $list = D('Orders')->getOrdersList($pageNo, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as &$val) {
                $orderListInfo = M('order_list')->where(['order_id' => $val['order_id']])->find();
                $info = D('Goods')->getGoodsInfo($orderListInfo['goods_id']);
                if (!empty($orderListInfo['goods_spec_id'])) {
                    $goodsListInfo = M('goods_list')->where(['goods_id' => $orderListInfo['goods_id'], 'goods_spec_id' => $orderListInfo['goods_spec_id']])->find();
                } else {
                    $goodsListInfo = M('goods_list')->where(['goods_id' => $orderListInfo['goods_id']])->find();
                }
                $val['name'] = $info['goods_title'];
                $val['img'] = empty($goodsListInfo['goods_img_url']) ? $info['goods_small_pic'] : $goodsListInfo['goods_img_url'];
                $val['prize'] = $goodsListInfo['selling_price'];
                $val['size'] = $goodsListInfo['goods_spec'];
                $val['nums'] = $orderListInfo['nums'];
                $orderExpressInfo = M('order_express')->where(['order_id' => $val['order_id']])->find();
                $val['delivery'] = $orderExpressInfo['express_title'];
                $val['delivery_id'] = $orderExpressInfo['express_no'];
                //评价
                if ($orderCommentInfo = M('order_comment')->where(['order_id' => $val['order_id']])->find()) {
                    if ($orderCommentInfo['status'] == 1) {
                        $val['pj_status'] = 1; //评价已完成
                    } else {
                        $val['pj_status'] = 2; //评价待审核
                    }
                } else {
                    $val['pj_status'] = 0; //待评价
                }
                if ($val['status'] == 5) {
                    $val['r_status'] = 0;
                    if ($orderRefundInfo = M('order_refund')->where(['order_id' => $val['order_id']])->find()) {
                        if ($orderRefundInfo['status'] == 6) {
                            $val['r_status'] = 1;
                        }
                    }
                }
            }
        }
        $return['code'] = 1;
        $return['errorMsg'] = $list;
        $return['userInfo'] = $rsInfo;
        $return['loadmore'] = (count($list) == $pageSize) ? true : false;
        $this->ajaxReturn($return);
    }

    public function getUserOrdersInfo()
    {
        $return = array();
        $filter = array();
        $id = I('id', '');
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $orderInfo = M('orders')->where(['id' => $id])->find();
        $addrInfo = M('order_address')->where(['order_id' => $orderInfo['order_id']])->find();
        $expressInfo = M('order_express')->where(['order_id' => $orderInfo['order_id']])->find();
        $orderInfo['delivery'] = $expressInfo['express_title'];
        $orderInfo['delivery_no'] = $expressInfo['express_no'];
        $itemList = M('order_list')->where(['order_id' => $orderInfo['order_id']])->select();
        if (is_array($itemList) && count($itemList) > 0) {
            foreach ($itemList as &$val) {
                if (!empty($val['goods_spec_id'])) {
                    $goodsListInfo = M('goods_list')->where(['goods_id' => $val['goods_id'], 'goods_spec_id' => $val['goods_spec_id']])->find();
                } else {
                    $goodsListInfo = M('goods_list')->where(['goods_id' => $val['goods_id']])->find();
                }
                $goodsInfo = M('goods')->where(['id' => $val['goods_id']])->find();
                $val['img'] = empty($goodsListInfo['goods_img_url']) ? $goodsInfo['goods_small_pic'] : $goodsListInfo['goods_img_url'];
                $val['price'] = $goodsListInfo['selling_price'];
                $val['goods_spec'] = $goodsListInfo['goods_spec'];
            }
        }
        if ($orderCommentInfo = M('order_comment')->where(['order_id' => $orderInfo['order_id']])->find()) {
            if ($orderCommentInfo['status'] == 1) {
                $orderInfo['pj_status'] = 1; //评价已完成
            } else {
                $orderInfo['pj_status'] = 2; //评价待审核
            }
        } else {
            $orderInfo['pj_status'] = 0; //待评价
        }
        $return['code'] = 1;
        $return['errorMsg'] = $orderInfo;
        $return['itemList'] = $itemList;
        $return['addrInfo'] = $addrInfo;
        $this->ajaxReturn($return);
    }

    public function getUserOrdersInfo2()
    {
        $return = array();
        $filter = array();
        $order_id = I('order_id', '');
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $orderInfo = M('orders')->where(['order_id' => $order_id])->find();
        $return['code'] = 1;
        $return['errorMsg'] = $orderInfo;
        $this->ajaxReturn($return);
    }

    //获取评论信息
    public function getOrderCommentInfo()
    {
        $return = array();
        $filter = array();
        $id = I('id', '');
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        if ($orderInfo = M('orders')->where(['id' => $id])->find()) {
            if ($orderCommentInfo = M('order_comment')->where(['order_id' => $orderInfo['order_id']])->find()) {
                $orderCommentInfo['imagesUrl'] = !empty($orderCommentInfo['images']) ? explode(',', $orderCommentInfo['images']) : [];
                $return['code'] = 1;
                $return['errorMsg'] = $orderCommentInfo;
                $this->ajaxReturn($return);
            }
        }
        $return['code'] = 0;
        $return['errorMsg'] = 'fail';
        $this->ajaxReturn($return);
    }

    //添加评论信息
    public function addOrderComment()
    {
        $return = array();
        $filter = array();
        $data = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $score = I('score', '');
        $data['level1'] = $score;
        $data['content'] = I('content', '');
        $id = I('id', '');
        if ($orderInfo = M('orders')->where(['id' => $id])->find()) {
            $data['user_id'] = $orderInfo['user_id'];
            $data['order_id'] = $orderInfo['order_id'];
            $data['addtime'] = date('Y-m-d H:i:s', time());
            $orderList = M('order_list')->where(['order_id' => $orderInfo['order_id']])->select();
            if (is_array($orderList) && count($orderList) > 0) {
                foreach ($orderList as $key => $val) {
                    $data['goods_id'] = $val['goods_id'];
                    $data['goods_list_id'] = $val['goods_list_id'];
                    $data['goods_spec_id'] = $val['goods_spec_id'];
                    M('order_comment')->add($data);
                }
                $return['code'] = 1;
                $return['errorMsg'] = '操作成功';
                $this->ajaxReturn($return);
            }
        }
        $return['code'] = 0;
        $return['errorMsg'] = '操作失败';
        $this->ajaxReturn($return);
    }

    /*
     * Function:		uploadOrderCommentImage
     * Description:	订单评论图片上传
     */
    public function uploadOrderCommentImage()
    {
        $data = array();
        $id = I('id');
        //设置编码防止乱码
        header("Content-Type:text/html;charset=utf-8");
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 10485760; // 设置附件上传大小 1M
        $upload->exts = array('jpg', 'jpeg', 'gif', 'png'); // 设置附件上传类
        $upload->savePath = '/order_comment/'; // 设置附件上传目录
        $upload->replace = true; //存在同名文件是否是覆盖，默认为false
        //上传文件
        $info = $upload->uploadOne($_FILES['uploadimg']);
        $filename = 'https://' . $_SERVER["SERVER_NAME"] . '/Uploads' . $info['savepath'] . $info['savename'];
        $exts = $info['ext'];
        if (!$info) { // 上传错误提示错误信息
            $data['code'] = 0;
            $data['errorMsg'] = '上传失败:' . $upload->getError();
        } else { //上传成功
            if ($orderInfo = M('orders')->where(['id' => $id])->find()) {
                if ($orderCommentInfo = M('order_comment')->where(['order_id' => $orderInfo['order_id']])->find()) {
                    if (!empty($orderCommentInfo['images'])) {
                        $imgUrl = $orderCommentInfo['images'] . ',' . $filename;
                    } else {
                        $imgUrl = $filename;
                    }
                    $list = M('order_comment')->where(['order_id' => $orderInfo['order_id']])->select();
                    foreach ($list as $ks => $vs) {
                        $upData = array();
                        $upData['id'] = $vs['id'];
                        $upData['images'] = $imgUrl;
                        D('OrderComment')->updateOrderComment($upData);
                    }
                    $data['code'] = 1;
                    $data['errorMsg'] = '操作成功';
                }
            } else {
                $data['code'] = 0;
                $data['errorMsg'] = '操作失败';
            }
        }
        $this->ajaxReturn($data);
    }

    /**
     * 【用户】获取用户订单评论
     * @return:array
     */
    public function getUserOrderCommentList()
    {
        $return = array();
        $filter = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $order_id = I('order_id', '');
        if (empty($order_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '缺少必要参数10001';
            $this->ajaxReturn($return);
        }
        $orderListInfo = M('order_list')->where(['order_id' => $order_id])->find();
        if (!empty($orderListInfo['goods_spec_id'])) {
            $goodsListInfo = M('goods_list')->where(['goods_id' => $orderListInfo['goods_id'], 'goods_spec_id' => $orderListInfo['goods_spec_id']])->find();
        } else {
            $goodsListInfo = M('goods_list')->where(['goods_id' => $orderListInfo['goods_id']])->find();
        }
        $goodsInfo = M('goods')->where(['id' => $orderListInfo['goods_id']])->find();
        $return['code'] = 1;
        $return['errorMsg'] = empty($goodsListInfo['goods_img_url']) ? $goodsInfo['goods_small_pic'] : $goodsListInfo['goods_img_url'];
        $this->ajaxReturn($return);
    }

    /**
     * 【用户】用户订单评论
     * @return:array
     */
    public function userOrderComment()
    {
        $return = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $evaluate = I('evaluate', 1);
        $level1 = I('level1', 0);
        $level2 = I('level2', 0);
        $level3 = I('level3', 0);
        $content = I('content', '');
        if (empty($content)) {
            $return['code'] = 0;
            $return['errorMsg'] = '评论内容不能为空';
            $this->ajaxReturn($return);
        }
        $images = I('imagesUrl', '');
        $order_id = I('order_id', '');
        if (empty($order_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '缺少必要参数10001';
            $this->ajaxReturn($return);
        }
        $orderListInfo = M('order_list')->where(['order_id' => $order_id])->find();
        $data = [];
        $data['user_id'] = $rsInfo['id'];
        $data['order_id'] = $order_id;
        $data['goods_id'] = $orderListInfo['goods_id'];
        $data['goods_list_id'] = $orderListInfo['goods_list_id'];
        $data['goods_spec_id'] = $orderListInfo['goods_spec_id'];
        $data['level1'] = $orderListInfo['level1'];
        $data['level2'] = $orderListInfo['level2'];
        $data['level3'] = $orderListInfo['level3'];
        $data['evaluate'] = $orderListInfo['evaluate'];
        $data['content'] = $orderListInfo['content'];
        $data['images'] = $orderListInfo['images'];
        $data['addtime'] = date('Y-m-d H:i:s', time());
        if (M('order_comment')->where(['user_id' => $rsInfo['id'], 'order_id' => $order_id])->find()) {
            $return['code'] = 0;
            $return['errorMsg'] = '您已评价过';
            $this->ajaxReturn($return);
        }
        $ret = M('order_comment')->add($data);
        if (!empty($ret)) {
            $return['code'] = 1;
            $return['errorMsg'] = 'success';
            $this->ajaxReturn($return);
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '评价失败';
            $this->ajaxReturn($return);
        }
    }

    /**
     * 【用户】用户取消订单
     * @return:array
     */
    public function cancelUserOrder()
    {
        $return = array();
        $filter = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $order_id = I('order_id', '');
        if (empty($order_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '缺少必要参数10001';
            $this->ajaxReturn($return);
        }
        if (!$orderInfo = M('orders')->where(['order_id' => $order_id])->find()) {
            $return['code'] = 0;
            $return['errorMsg'] = '不存在此订单';
            $this->ajaxReturn($return);
        }
        if ($orderInfo['status'] > 1) {
            $return['code'] = 0;
            $return['errorMsg'] = '订单状态不对，无法取消订单';
            $this->ajaxReturn($return);
        }
        $ret = M('orders')->where(['order_id' => $order_id])->save(['status' => 5]);
        if (!empty($ret)) {
            $return['code'] = 1;
            $return['errorMsg'] = '取消成功';
            $this->ajaxReturn($return);
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '取消失败';
            $this->ajaxReturn($return);
        }
    }

    /**
     * 【用户】用户订单确认收货
     * @return:array
     */
    public function confirmUserOrder()
    {
        $return = array();
        $filter = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $order_id = I('order_id', '');
        if (empty($order_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '缺少必要参数10001';
            $this->ajaxReturn($return);
        }
        if (!$orderInfo = M('orders')->where(['order_id' => $order_id])->find()) {
            $return['code'] = 0;
            $return['errorMsg'] = '不存在此订单';
            $this->ajaxReturn($return);
        }
        if ($orderInfo['status'] != 3) {
            $return['code'] = 0;
            $return['errorMsg'] = '订单状态不对，无法确认收货';
            $this->ajaxReturn($return);
        }
        //检查是否有退款
        if (M('order_refund')->where(['order_id' => $order_id, 'status' => ['not in', [6, 7]]])->find()) {
            $return['code'] = 0;
            $return['errorMsg'] = '无法操作，此订单有退款订单在进行';
            $this->ajaxReturn($return);
        }
        $ret = M('orders')->where(['order_id' => $order_id])->save(['status' => 4]);
        if (!empty($ret)) {
            if ($userInfo = M('user')->where(['id' => $orderInfo['user_id']])->find()) {
                //增加积分记录
                $integral = [];
                $integral['type'] = 1;
                $integral['user_id'] = $userInfo['id'];
                $integral['title'] = '用户下单增加积分';
                $integral['order_id'] = $orderInfo['order_id'];
                $integral['integral'] = intval($orderInfo['total_integral']);
                $integral['addtime'] = date('Y-m-d H:i:s', time());
                M('user_integral_logs')->add($integral);
                //增加用户积分
                $uData = array();
                $uData['integral'] = array('exp', 'integral+' . $integral['integral']);
                M('user')->where(['id' => $orderInfo['user_id']])->save($uData);
            }
            $return['code'] = 1;
            $return['errorMsg'] = '操作成功';
            $this->ajaxReturn($return);
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '操作失败';
            $this->ajaxReturn($return);
        }
    }

    /**
     * 购物车-列表
     * @return:array
     */
    public function orderCartList()
    {
        $return = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $user_id = $rsInfo['id'];
        $carts = [];
        $list = M('order_cart')->where(['user_id' => $user_id, 'status' => 1])->select();
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => $val) {
                if (!empty($val['goods_spec_id'])) {
                    $goodsListInfo = M('goods_list')->where(['goods_id' => $val['goods_id'], 'goods_spec_id' => $val['goods_spec_id']])->find();
                } else {
                    $goodsListInfo = M('goods_list')->where(['goods_id' => $val['goods_id']])->find();
                }
                $goodsInfo = M('goods')->where(['id' => $val['goods_id']])->find();
                $carts[$key]['goods_id'] = $val['goods_id'];
                $carts[$key]['goods_list_id'] = $val['goods_list_id'];
                $carts[$key]['goods_spec_id'] = $val['goods_spec_id'];
                $carts[$key]['name'] = $goodsInfo['goods_title'];
                $carts[$key]['size'] = $goodsListInfo['goods_spec'];
                $carts[$key]['prize'] = $goodsListInfo['selling_price'];
                $carts[$key]['img'] = empty($goodsListInfo['goods_img_url']) ? $goodsInfo['goods_small_pic'] : $goodsListInfo['goods_img_url'];
                $carts[$key]['num'] = $val['nums'];
                $carts[$key]['id'] = $val['id'];
                $carts[$key]['user_id'] = $val['user_id'];
                $carts[$key]['integral'] = $goodsInfo['integral'];
            }
        }
        $return['code'] = 1;
        $return['errorMsg'] = $carts;
        $this->ajaxReturn($return);
    }

    /**
     * 购物车
     * @return:array
     */
    public function orderCart()
    {
        $return = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $data = [];
        $data['user_id'] = $rsInfo['id'];
        $data['goods_id'] = I('goods_id', '');
        $data['goods_list_id'] = I('goods_list_id', '');
        $data['goods_spec_id'] = I('goods_spec_id', '');
        $data['nums'] = I('nums', 0);
        $data['addtime'] = date('Y-m-d H:i:s', time());
        $ret = D('OrderCart')->orderCart($data);
        $this->ajaxReturn($ret);
    }

    /**
     * 直接购买
     * @return:array
     */
    public function directBuyOrder()
    {
        $return = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $data = [];
        $data['user_id'] = $rsInfo['id'];
        $data['goods_id'] = I('goods_id', '');
        $data['goods_list_id'] = I('goods_list_id', '');
        $data['goods_spec_id'] = I('goods_spec_id', '');
        $data['nums'] = I('nums', 0);
        $ret = D('Orders')->directBuyOrder($data);
        $this->ajaxReturn($ret);
    }

    /**
     * 购物车-删除
     * @return:array
     */
    public function delOrderCart()
    {
        $return = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $data = [];
        $data['id'] = I('id', '');
        $ret = D('OrderCart')->delOrderCart($data);
        $this->ajaxReturn($ret);
    }

    /**
     * 购物车-提交
     * @return:array
     */
    public function commitOrderCart()
    {
        $return = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $data = [];
        $data['user_id'] = $rsInfo['id'];
        $data['goods_data'] = I('goods_data', '');
        $ret = D('OrderCart')->commitOrderCart($data);
        $this->ajaxReturn($ret);
    }

    /* 
     * Function:addOrder
     * Description:增加订单
     */
    public function addOrder()
    {
        $return = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $user_addr_id = I('user_addr_id', '');
        if (empty($user_addr_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '请选择用户收货地址';
            $this->ajaxReturn($return);
        }
        $data = [];
        $data['user_id'] = $rsInfo['id'];
        $data['user_addr_id'] = $user_addr_id;
        $data['coupon_id'] = I('coupon_id', '');
        $data['coupon_amount'] = I('coupon_amount', 0);
        $data['total_integral'] = I('total_integral', 0);
        $goods_data = I('goods_data', '');
        $goods_data = html_entity_decode($goods_data);
        $goods_data = json_decode($goods_data, true);
        $data['goods_data'] = $goods_data;
        $ret = D('Orders')->addUserOrders($data);
        if ($ret['code'] == 1) {
            $payInfo = [];
            $payInfo['sessionid'] = $sessionid;
            $payInfo['order_id'] = $ret['order_id'];
            $payInfo['price'] = $ret['price'];
            $ret = $this->wxPay($payInfo);
            if ($ret['code'] == 1) {
                $return['code'] = 1;
                $return['errorMsg'] = $ret;
                $return['order_id'] = $payInfo['order_id'];
                $this->ajaxReturn($return);
            } else {
                $return['code'] = 0;
                $return['errorMsg'] = $ret['errorMsg'];
                $this->ajaxReturn($return);
            }
        }
        $this->ajaxReturn($ret);
    }

    /* 
     * Function:payOrder
     * Description:支付订单
     */
    public function payOrder()
    {
        $return = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $order_id = I('order_id', '');
        if (empty($order_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '缺少订单号';
            $this->ajaxReturn($return);
        }
        $price = I('price', '');
        if (empty($price)) {
            $return['code'] = 0;
            $return['errorMsg'] = '缺少订单金额';
            $this->ajaxReturn($return);
        }
        $payInfo = [];
        $payInfo['sessionid'] = $sessionid;
        $payInfo['order_id'] = $order_id;
        $payInfo['price'] = $price;
        $ret = $this->wxPay($payInfo);
        if ($ret['code'] == 1) {
            $return['code'] = 1;
            $return['errorMsg'] = $ret;
            $this->ajaxReturn($return);
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = $ret['errorMsg'];
            $this->ajaxReturn($return);
        }
        $this->ajaxReturn($ret);
    }

    /*
     * Function:wxPay
     * Description:微信支付
     */
    public function wxPay($payInfo = '')
    {
        $return = array();
        $postData = $payInfo;
        if ($rsInfo = D('User')->getUsersInfoBySessionId($postData['sessionid'])) {
            $fee = $postData['price']; //金额
            $appid = C('appid'); //小程序appid
            $body = '国成服饰'; //支付简单描述
            $mch_id = C('mch_id'); //微信支付分配的商户号
            $nonce_str = wxNonceStr(); //随机字符串
            $notify_url = C('hostName') . '/WebChatApi/wxPayCallBack'; //回调url地址
            $openid = $rsInfo['openid']; //用户openid
            $out_trade_no = $postData['order_id']; //商户订单号
            $spbill_create_ip = $_SERVER['SERVER_ADDR']; //服务器的ip
            $total_fee = $fee * 100;
            $trade_type = 'JSAPI'; //交易类型 默认
            //sign签名内容
            $data = array();
            $data['appid'] = $appid;
            $data['body'] = $body;
            $data['mch_id'] = $mch_id;
            $data['nonce_str'] = $nonce_str;
            $data['notify_url'] = $notify_url;
            $data['openid'] = $openid;
            $data['out_trade_no'] = $out_trade_no;
            $data['spbill_create_ip'] = $spbill_create_ip;
            $data['total_fee'] = $total_fee;
            $data['trade_type'] = $trade_type;
            $sign = wxSign($data);
            $postXml = '<xml>
			<appid>' . $appid . '</appid>
			<body>' . $body . '</body>
			<mch_id>' . $mch_id . '</mch_id>
			<nonce_str>' . $nonce_str . '</nonce_str>
			<notify_url>' . $notify_url . '</notify_url>
			<openid>' . $openid . '</openid>
			<out_trade_no>' . $out_trade_no . '</out_trade_no>
			<spbill_create_ip>' . $spbill_create_ip . '</spbill_create_ip>
			<total_fee>' . $total_fee . '</total_fee>
			<trade_type>' . $trade_type . '</trade_type>
			<sign>' . $sign . '</sign>
			</xml> ';
            $xml = curl_post(C('wxPrepayIdUrl'), $postXml);
            $returnData = xmltoarray($xml);
            //\Think\Log::write('微信支付提交信息：'.serialize($data).'|'.$xml.'|'.$postXml,'INFO');
            if ($returnData['return_code'] == 'SUCCESS' && $returnData['result_code'] == 'SUCCESS') {
                $time = time();
                $tmpArr = array();
                $data = array();
                $tmpArr['appId'] = $appid;
                $tmpArr['nonceStr'] = $nonce_str;
                $tmpArr['package'] = 'prepay_id=' . $returnData['prepay_id'];
                $tmpArr['signType'] = 'MD5';
                $tmpArr['timeStamp'] = $time;
                //返回正确参数
                $return['code'] = 1;
                $return['timeStamp'] = "$time";
                $return['nonceStr'] = $nonce_str;
                $return['package'] = $tmpArr['package'];
                $return['signType'] = 'MD5';
                $return['paySign'] = wxSign($tmpArr);
            } else {
                $return['code'] = 0;
                $return['errorMsg'] = '支付失败';
                $return['return_code'] = $returnData['return_code'];
                $return['return_msg'] = $returnData['return_msg'];
            }
        } else {
            $return['code'] = 0;
            $return['errorMsg'] = '用户不存在';
        }
        return $return;
        //$this->ajaxReturn($return);
    }

    /*
     * Function:wxPayCallBack
     * Description:微信支付回调url
     */
    function wxPayCallBack()
    {
        $upArr = array();
        $payArr = array();
        $xml = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
        $result = true;
        //\Think\Log::write($xml,'INFO');
        $data = xmltoarray($xml);
        $dataSign = $data['sign'];
        unset($data['sign']);
        $sign = wxSign($data);
        //\Think\Log::write('加签签名:'.serialize($sign),'INFO');
        if (($sign === $dataSign) && ($data['return_code'] == 'SUCCESS') && ($data['result_code'] == 'SUCCESS')) {
            $order_id = $data['out_trade_no']; //订单单号
            $openid = $data['openid']; //付款人openID
            $total_fee = $data['total_fee']; //付款金额
            $transaction_id = $data['transaction_id']; //微信支付流水号
            //更新订单
            $upArr = [];
            $upArr['is_pay'] = 1;
            $upArr['pay_no'] = $transaction_id;
            $upArr['pay_price'] = round(($total_fee / 100), 2);
            $upArr['pay_time'] = date('Y-m-d H:i:s', time());
            $upArr['status'] = 2;
            M('orders')->where(array('order_id' => $order_id))->save($upArr);
            $itemList = M('order_list')->where(['order_id' => $order_id])->select();
            if (is_array($itemList) && count($itemList) > 0) {
                foreach ($itemList as $key => $val) {
                    M('goods')->where(['id' => $val['goods_id']])->save(['sale_nums' => ['exp', 'sale_nums+' . $val['nums']]]);
                }
            }
        } else {
            $result = false;
        }
        if ($result) {
            $str = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
        } else {
            $str = '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>';
        }
        echo $str;
    }

    /**
     * 更新用户昵称头像信息
     * @return:array
     */
    public function updateUserPhotoInfo()
    {
        $return = array();
        $data = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $sex = I('sex', '');
        $mobile = I('mobile', '');
        $data['id'] = $rsInfo['id'];
        $data['nickname'] = I('nickname', '');
        $data['username'] = $data['nickname'];
        if (!empty($sex)) {
            $data['sex'] = $sex;
        }
        if (empty($data['nickname'])) {
            $return['code'] = 0;
            $return['errorMsg'] = '请填写用户昵称';
            $this->ajaxReturn($return);
        }
        if (empty($mobile)) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '手机号不能为空']);
        }
        $data['mobile'] = $mobile;
        if ($userInfo = M('user')->where(['mobile' => $mobile, 'status' => 1, 'is_deleted' => 0])->find()) {
            if (empty($userInfo['sessionid'])) {
                $uData = [];
                $uData['openid'] = $rsInfo['openid'];
                $uData['unionid'] = $rsInfo['unionid'];
                $uData['session_key'] = $rsInfo['session_key'];
                $uData['sessionid'] = $rsInfo['sessionid'];
                $ret = M('user')->where(['id' => $userInfo['id']])->save($uData);
                if ($ret !== false) {
                    M('user')->where(['id' => $rsInfo['id']])->delete();
                    $this->ajaxReturn(['code' => 1, 'errorMsg' => $userInfo['id']]);
                } else {
                    $this->ajaxReturn(['code' => 0, 'errorMsg' => '提交失败']);
                }
            } else {
                $ret = M('user')->where(['id' => $userInfo['id']])->save($data);
                if ($ret !== false) {
                    $this->ajaxReturn(['code' => 1, 'errorMsg' => $userInfo['id']]);
                } else {
                    $this->ajaxReturn(['code' => 0, 'errorMsg' => '提交失败']);
                }
            }
        } else {
            $ret = M('user')->where(['id' => $data['id']])->save($data);
            if ($ret !== false) {
                $this->ajaxReturn(['code' => 1, 'errorMsg' => $data['id']]);
            } else {
                $this->ajaxReturn(['code' => 0, 'errorMsg' => '提交失败']);
            }
        }
    }

    /*
     * Function:		uploadUserPhotoImage
     * Description:	用户头像图片上传
     */
    public function uploadUserPhotoImage()
    {
        $data = array();
        $id = I('id');
        //设置编码防止乱码
        header("Content-Type:text/html;charset=utf-8");
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 10485760 * 2; // 设置附件上传大小 2M
        $upload->exts = array('jpg', 'jpeg', 'gif', 'png'); // 设置附件上传类
        $upload->savePath = '/user/'; // 设置附件上传目录
        $upload->replace = true; //存在同名文件是否是覆盖，默认为false
        //上传文件
        $info = $upload->uploadOne($_FILES['uploadimg']);
        $filename = 'https://' . $_SERVER["SERVER_NAME"] . '/Uploads' . $info['savepath'] . $info['savename'];
        $exts = $info['ext'];
        if (!$info) { // 上传错误提示错误信息
            $data['code'] = 0;
            $data['errorMsg'] = '上传失败:' . $upload->getError();
        } else { //上传成功
            $ret = M('user')->where(['id' => $id])->save(['avatarurl' => $filename]);
            if ($ret !== false) {
                $data['code'] = 1;
                $data['errorMsg'] = '操作成功';
            } else {
                $data['code'] = 0;
                $data['errorMsg'] = '操作失败';
            }
        }
        $this->ajaxReturn($data);
    }

    /**
     * 【用户】获取用户卡片列表
     * @return:array
     */
    public function getVipCardList()
    {
        $return = array();
        $filter = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $filter['user_id'] = $rsInfo['id'];
        $pageNo = I('pageNo', 1);
        $pageSize = I('pageSize', 10);
        $list = D('UserCard')->getUserCardList($pageNo, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as &$val) {
                $val['price'] = $val['price'] * 1;
                $goodsInfo = M('goods')->where(['id' => $val['goods_id']])->find();
                $val['goods_name'] = $goodsInfo['goods_title'];
                $val['cate_id'] = ($goodsInfo['cate_id'] == 2) ? 1 : 2;
                $cateInfo = M('goods_cate')->where(['id' => $goodsInfo['cate_id']])->find();
                $val['cate_name'] = $cateInfo['cate_name'];
                $val['cate_image'] = $cateInfo['cate_small_pic'];
                $val['total_nums'] = $goodsInfo['total_nums'];
                $val['end_time'] = date('Y-m-d', strtotime($val['end_time']));
                $val['color'] = '#326eac';
                $val['color2'] = '#2a5c90';
                if ($val['cate_id'] == 1) {
                    $val['color'] = '#ad800e';
                    $val['color2'] = '#cc970e';
                }
            }
        }
        $return['code'] = 1;
        $return['errorMsg'] = $list;
        $return['userInfo'] = $rsInfo;
        $return['loadmore'] = (count($list) == $pageSize) ? true : false;
        $this->ajaxReturn($return);
    }

    /**
     * 【用户】获取用户卡片详情
     * @return:array
     */
    public function getVipCardInfo()
    {
        $return = array();
        $filter = array();
        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }
        $id = I('id/d', '');
        if (!$info = M('user_card')->where(['id' => $id])->find()) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '不存在此会员卡']);
        }
        $info['price'] = $info['price'] * 1;
        $goodsInfo = M('goods')->where(['id' => $info['goods_id']])->find();
        $info['goods_name'] = $goodsInfo['goods_title'];
        $info['cate_id'] = ($goodsInfo['cate_id'] == 2) ? 1 : 2;
        $cateInfo = M('goods_cate')->where(['id' => $goodsInfo['cate_id']])->find();
        $info['cate_name'] = $cateInfo['cate_name'];
        $info['cate_image'] = $cateInfo['cate_small_pic'];
        $info['total_nums'] = $goodsInfo['total_nums'];
        $info['begin_time'] = date('Y-m-d', strtotime($info['begin_time']));
        $info['end_time'] = date('Y-m-d', strtotime($info['end_time']));
        $info['color'] = '#326eac';
        $info['color2'] = '#2a5c90';
        if ($info['cate_id'] == 1) {
            $info['color'] = '#ad800e';
            $info['color2'] = '#cc970e';
        }
        $this->ajaxReturn(['code' => 1, 'errorMsg' => $info]);
    }

    /**
     * 【预约】获取课程分类列表
     * @return:array
     */
    public function getCourseCateList()
    {
        $return = array();
        $filter = array();
        $filter['title'] = I('title', '');
        $filter['id'] = I('id', 0);
        $pageNo = I('pageNo', 1);
        $pageSize = I('pageSize', 10);
        $list = D('CourseCate')->getCourseCateList($pageNo, $filter, $pageSize);
        $return['code'] = 1;
        $return['data'] = $list;
        $return['loadmore'] = (count($list) == $pageSize) ? true : false;
        $this->ajaxReturn($return);
    }

    /**
     * 【预约】获取课程列表
     * @return:array
     */
    public function getCourseList()
    {
        $return = array();
        $filter = array();
        $filter['course_date'] = I('course_date', '');
        $filter['cate_id'] = I('cate_id', 0);
        $pageNo = I('pageNo', 1);
        $pageSize = I('pageSize', 10);
        $list = D('Course')->getCourseList($pageNo, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => &$val) {
                $val['begin_time'] = date('H:i', strtotime($val['begin_time']));
                $val['end_time'] = date('H:i', strtotime($val['end_time']));
                $val['date'] = $val['course_date'];
                $val['coach_name'] = M('stores_worker_list')->where(['id' => $val['coach_id'], 'role' => 1])->getField('name');
                $val['classroom_name'] = M('stores_classroom_list')->where(['id' => $val['classroom_id']])->getField('name');
            }
        }
        $return['code'] = 1;
        $return['data'] = $list;
        $return['loadmore'] = (count($list) == $pageSize) ? true : false;
        $this->ajaxReturn($return);
    }

    /**
     * 【预约】获取课程详情
     * @return:array
     */
    public function getCourseInfo()
    {
        $return = array();
        $id = I('id', 0);
        $info = D('Course')->getCourseInfo($id);
        $info['coach_name'] = M('stores_worker_list')->where(['id' => $info['coach_id'], 'role' => 1])->getField('name');
        $info['classroom_name'] = M('stores_classroom_list')->where(['id' => $info['classroom_id']])->getField('name');
        $info['book_content'] = "1.本课程需早于开课前" . C('book_content') . "分钟预约
2.预约成功后自动扣除相应费用。
3.开课前{$info['cancel_time']}分钟不可取消
4.取消预约不扣除费用。";
        $info['queue_content'] = "1.本课程需早于开课前" . C('queue_content') . "分钟排队。
2.开课前" . C('queue_content') . "分钟停止从排队中加入预约。
3.排队上限为{$info['t_nums']}人。
4.排队不扣除余额或次数";
        $return['code'] = 1;
        $return['data'] = $info;
        $this->ajaxReturn($return);
    }

    /**
     * 【预约】提交预约确认
     * @return:array
     */
    public function courseOrderConfirm()
    {
        $return = array();
        $course_id = I('course_id/d', 0);
        $nums = I('nums/d', 0);
        if (!$course_info = D('Course')->getCourseInfo($course_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程不存在';
            $this->ajaxReturn($return);
        }
        $today = date('Y-m-d');
        if ($today > $course_info['course_date']) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程已过期';
            $this->ajaxReturn($return);
        }

        $exit_course = M('user_course')->where(['course_id' => $course_id, 'user_id' => $this->user_id, 'status' => ['neq', 5], 'is_deleted' => 0])->find();
        if ($exit_course) {
            $return['code'] = 0;
            $return['errorMsg'] = '您已经预约该课或者排队中';
            $this->ajaxReturn($return);
        }

        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }

        $order_num = M('user_course')->where(['course_id' => $course_id, 'status' => 1, 'is_deleted' => 0])->sum('nums'); //已经被预约次数
        if ($course_info['nums'] < ($order_num + $nums)) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程预约次数达到上限';
            $this->ajaxReturn($return);
        }
        $now = time();
        $begin_time = strtotime($course_info['begin_time']);
        $can_order_time = ($begin_time - $now) / 60; //距离开始时间所剩分钟数

        if ($today == $course_info['course_date']) {
            if ($can_order_time < C('book_content')) {
                $return['code'] = 0;
                $return['errorMsg'] = '该课程过可预约时间';
                $this->ajaxReturn($return);
            }
        }

        if (!$info = M('user_card')->where(['user_id' => $this->user_id])->find()) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '不存在此会员卡']);
        }
        $use_num = $info['nums'] - $info['freez'];
        if ($use_num < ($nums * $course_info['card_nums'])) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '您会员卡次数不足']);
        }

        $return['code'] = 1;
        $return['data'] = [
            'course_data' => $course_info,
            'member_data' => $rsInfo
        ];
        $this->ajaxReturn($return);
    }

    /**
     * 【预约】提交排队确认
     * @return:array
     */
    public function queueOrderConfirm()
    {
        $return = array();
        $course_id = I('course_id/d', 0);
        $nums = I('nums/d', 0);
        if (!$course_info = D('Course')->getCourseInfo($course_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程不存在';
            $this->ajaxReturn($return);
        }
        $today = date('Y-m-d');
        if ($today > $course_info['course_date']) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程已过期';
            $this->ajaxReturn($return);
        }

        $exit_course = M('user_course')->where(['course_id' => $course_id, 'user_id' => $this->user_id, 'status' => ['neq', 5], 'is_deleted' => 0])->find();
        if ($exit_course) {
            $return['code'] = 0;
            $return['errorMsg'] = '您已经预约该课或者排队中';
            $this->ajaxReturn($return);
        }

        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }

        $queue_num = M('user_course')->where(['course_id' => $course_id, 'status' => 3, 'is_deleted' => 0])->sum('nums'); //已经被排队次数
        if ($course_info['t_nums'] < ($queue_num + $nums)) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程排队次数达到上限';
            $this->ajaxReturn($return);
        }
        $now = time();
        $begin_time = strtotime($course_info['begin_time']);
        $can_queue_time = ($begin_time - $now) / 60; //距离开始时间所剩分钟数
        if ($today == $course_info['course_date']) {
            if ($can_queue_time < C('queue_content')) {
                $return['code'] = 0;
                $return['errorMsg'] = '该课程过可排队时间';
                $this->ajaxReturn($return);
            }
        }

        if (!$info = M('user_card')->where(['user_id' => $this->user_id])->find()) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '不存在此会员卡']);
        }
        $use_num = $info['nums'] - $info['freez'];
        if ($use_num < ($nums * $course_info['card_nums'])) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '您会员卡次数不足']);
        }

        $return['data'] = [
            'course_data' => $course_info,
            'member_data' => $rsInfo
        ];
        $this->ajaxReturn($return);
    }

    /**
     * 【预约】提交预约
     * @return:array
     */
    public function courseOrder()
    {
        $return = array();
        $course_id = I('course_id/d', 0);
        $nums = I('nums/d', 0);
        if (!$course_info = D('Course')->getCourseInfo($course_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程不存在';
            $this->ajaxReturn($return);
        }
        $today = date('Y-m-d');
        if ($today > $course_info['course_date']) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程已过期';
            $this->ajaxReturn($return);
        }

        $exit_course = M('user_course')->where(['course_id' => $course_id, 'user_id' => $this->user_id, 'status' => ['neq', 5], 'is_deleted' => 0])->find();
        if ($exit_course) {
            $return['code'] = 0;
            $return['errorMsg'] = '您已经预约该课或者排队中';
            $this->ajaxReturn($return);
        }

        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }

        $order_num = M('user_course')->where(['course_id' => $course_id, 'status' => 1, 'is_deleted' => 0])->sum('nums'); //已经被预约次数
        if ($course_info['nums'] < ($order_num + $nums)) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程预约次数达到上限';
            $this->ajaxReturn($return);
        }
        $now = time();
        $begin_time = strtotime($course_info['begin_time']);
        $can_order_time = ($begin_time - $now) / 60; //距离开始时间所剩分钟数
        if ($today == $course_info['course_date']) {
            if ($can_order_time < C('book_content')) {
                $return['code'] = 0;
                $return['errorMsg'] = '该课程过可预约时间';
                $this->ajaxReturn($return);
            }
        }

        if (!$info = M('user_card')->where(['user_id' => $this->user_id])->find()) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '不存在此会员卡']);
        }
        $use_num = $info['nums'] - $info['freez'];
        if ($use_num < ($nums * $course_info['card_nums'])) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '您会员卡次数不足']);
        }

        $order_data = [
            'store_id' => $course_info['store_id'],
            'course_id' => $course_info['id'],
            'user_id' => $this->user_id,
            'nums' => $nums,
            'status' => 1
        ];
        D('UserCourse')->addUserCourse($order_data);
        M('user_card')->where(['user_id' => $this->user_id])->setInc('freez', ($nums * $course_info['card_nums']));

        $return['code'] = 1;
        $return['data'] = 'success';
        $this->ajaxReturn($return);
    }

    /**
     * 【预约】提交排队
     * @return:array
     */
    public function queueOrder()
    {
        $return = array();
        $course_id = I('course_id/d', 0);
        $nums = I('nums/d', 0);
        if (!$course_info = D('Course')->getCourseInfo($course_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程不存在';
            $this->ajaxReturn($return);
        }

        $today = date('Y-m-d');
        if ($today > $course_info['course_date']) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程已过期';
            $this->ajaxReturn($return);
        }

        $exit_course = M('user_course')->where(['course_id' => $course_id, 'user_id' => $this->user_id, 'status' => ['neq', 5], 'is_deleted' => 0])->find();
        if ($exit_course) {
            $return['code'] = 0;
            $return['errorMsg'] = '您已经预约该课或者排队中';
            $this->ajaxReturn($return);
        }

        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }

        $queue_num = M('user_course')->where(['course_id' => $course_id, 'status' => 3, 'is_deleted' => 0])->sum('nums'); //已经被排队次数
        if ($course_info['t_nums'] < ($queue_num + $nums)) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程排队次数达到上限';
            $this->ajaxReturn($return);
        }
        $now = time();
        $begin_time = strtotime($course_info['begin_time']);
        $can_queue_time = ($begin_time - $now) / 60; //距离开始时间所剩分钟数
        if ($today == $course_info['course_date']) {
            if ($can_queue_time < C('queue_content')) {
                $return['code'] = 0;
                $return['errorMsg'] = '该课程过可排队时间';
                $this->ajaxReturn($return);
            }
        }

        if (!$info = M('user_card')->where(['user_id' => $this->user_id])->find()) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '不存在此会员卡']);
        }
        $use_num = $info['nums'] - $info['freez'];
        if ($use_num < ($nums * $course_info['card_nums'])) {
            $this->ajaxReturn(['code' => 0, 'errorMsg' => '您会员卡次数不足']);
        }

        $order_data = [
            'store_id' => $course_info['store_id'],
            'course_id' => $course_info['id'],
            'user_id' => $this->user_id,
            'nums' => $nums,
            'status' => 3,
            'queue_time' => date('Y-m-d H:i:s')
        ];
        D('UserCourse')->addUserCourse($order_data);

        $return['code'] = 1;
        $return['data'] = 'success';
        $this->ajaxReturn($return);
    }

    /**
     * 【预约】获取已订列表
     * @return:array
     */
    public function getCourseOrderList()
    {
        $return = array();
        $filter = array();
        $filter['user_id'] = $this->user_id;
        $filter['status'] = I('status', 0);
        $pageNo = I('pageNo', 1);
        $pageSize = I('pageSize', 10);
        $list = D('UserCourse')->getUserCourseList($pageNo, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => &$val) {
                $val['coach_name'] = M('stores_worker_list')->where(['id' => $val['coach_id'], 'role' => 1])->getField('name');
                $val['classroom_name'] = M('stores_classroom_list')->where(['id' => $val['classroom_id']])->getField('name');
                $val['numsd'] = M('user_course')->where(['status' => 1, 'course_id' => $val['course_id'], 'is_deleted' => 0])->sum('nums'); //已预约人数
                $val['t_numsd'] = M('user_course')->where(['status' => 3, 'course_id' => $val['course_id'], 'is_deleted' => 0])->sum('nums'); //已排队人数
            }
        }
        $return['code'] = 1;
        $return['data'] = $list;
        $return['loadmore'] = (count($list) == $pageSize) ? true : false;
        $this->ajaxReturn($return);
    }

    /**
     * 【已订】取消操作
     * @return:array
     */
    public function cancelOrder()
    {
        $course_id = I('course_id/d', 0);
        $user_id = $this->user_id;
        if (!$course_info = D('Course')->getCourseInfo($course_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程不存在';
            $this->ajaxReturn($return);
        }

        if (!$rsInfo = D('User')->getUserInfo($this->user_id)) {
            $return['code'] = 0;
            $return['errorMsg'] = '此用户不存在';
            $this->ajaxReturn($return);
        }

        $user_course_info = M('user_course')->where(['course_id' => $course_id, 'user_id' => $user_id, 'is_deleted' => 0])->find();
        if (!in_array($user_course_info['status'], [1, 3])) {
            $return['code'] = 0;
            $return['errorMsg'] = '此订单不可取消';
            $this->ajaxReturn($return);
        }

        $now = time();
        $begin_time = strtotime($course_info['begin_time']);
        $can_queue_time = ($begin_time - $now) / 60; //距离开始时间所剩分钟数
        $today = date('Y-m-d');
        if ($today == $course_info['course_date']) {
            if ($can_queue_time < $course_info['cancel_time']) {
                $return['code'] = 0;
                $return['errorMsg'] = '该课程过可取消时间';
                $this->ajaxReturn($return);
            }
        }
        M('user_course')->where(['course_id' => $course_id, 'user_id' => $user_id])->save(['status' => 5, 'cancel_time' => date('Y-m-d H:i:s')]);
        if ($user_course_info['status'] == 1) {
            M('user_card')->where(['user_id' => $this->user_id])->setDec('freez', ($user_course_info['nums'] * $course_info['card_nums']));
        }
        $return['code'] = 1;
        $return['data'] = 'success';
        $this->ajaxReturn($return);
    }

    /**
     * 【预约】我的卡券列表
     * @return:array
     */
    public function getCourseCardList()
    {
        $return = array();
        $filter = array();
        $filter['user_id'] = $this->user_id;
        $pageNo = I('pageNo', 1);
        $pageSize = I('pageSize', 10);
        $list = D('UserCourse')->getUserCourseList($pageNo, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => &$val) {
                $val['coach_name'] = M('stores_worker_list')->where(['id' => $val['coach_id'], 'role' => 1])->getField('name');
                $val['classroom_name'] = M('stores_classroom_list')->where(['id' => $val['classroom_id']])->getField('name');
                $val['name'] = $this->name;
            }
        }
        $return['code'] = 1;
        $return['data'] = $list;
        $return['loadmore'] = (count($list) == $pageSize) ? true : false;
        $this->ajaxReturn($return);
    }

    /**
     * 【预约】获取请假列表
     * @return:array
     */
    public function getCourseOrderLeaveList()
    {
        $return = array();
        $filter = array();
        $filter['user_id'] = $this->user_id;
        $filter['status'] = 4;
        $pageNo = I('pageNo', 1);
        $pageSize = I('pageSize', 10);
        $list = D('UserCourseLeave')->getUserCourseLeaveList($pageNo, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => &$val) {
                $val['coach_name'] = M('stores_worker_list')->where(['id' => $val['coach_id'], 'role' => 1])->getField('name');
                $val['classroom_name'] = M('stores_classroom_list')->where(['id' => $val['classroom_id']])->getField('name');
            }
        }
        $return['code'] = 1;
        $return['data'] = $list;
        $return['loadmore'] = (count($list) == $pageSize) ? true : false;
        $this->ajaxReturn($return);
    }

    /**
     * 【合同】获取我的合同
     * @return:array
     */
    public function getContract()
    {
        $return['code'] = 1;
        $return['data'] = $this->contract;
        $this->ajaxReturn($return);
    }

    /**
     * 【教练端】教练页面统计
     */
    public function coachInfo()
    {
        if ($this->role != 1) {
            $return['code'] = 0;
            $return['errorMsg'] = '您无权查看';
            $this->ajaxReturn($return);
        }
        if (!$coach_info = M('stores_worker_list')->where(['mobile' => $this->mobile])->find()) {
            $return['code'] = 0;
            $return['errorMsg'] = '该教练不存在';
            $this->ajaxReturn($return);
        }
        $today = date("Y-m-d");
        $today_course = M('course')->where(['coach_id' => $coach_info['id'], 'course_date' => $today, 'status' => 1, 'is_deleted' => 0])->count();
        $thisweek_start = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y")));
        $thisweek_end = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y")));
        $week_course = M('course')->where(['coach_id' => $coach_info['id'], 'course_date' => ['between', [$thisweek_start, $thisweek_end]], 'status' => 1, 'is_deleted' => 0])->count();
        $member_nums = M('user_course')->alias('a')->join('left join course b on a.course_id=b.id')->where(['b.coach_id' => $coach_info['id'], 'a.status' => ['in', [1, 2]], 'a.is_deleted' => 0])->group('a.user_id')->count();
        $return['code'] = 1;
        $return['data'] = [
            'statistical' => ['today_course' => $today_course, 'week_course' => $week_course, 'member_nums' => $member_nums],
            'coach_data' => $coach_info
        ];
        $this->ajaxReturn($return);
    }

    /**
     * 【教练端】教练页面课程
     * @param：course_date:课程日期 Y-m-d
     * @param：cate_id:课程分类id
     */
    public function coachCourse()
    {
        if ($this->role != 1) {
            $return['code'] = 0;
            $return['errorMsg'] = '您无权查看';
            $this->ajaxReturn($return);
        }
        if (!$coach_info = M('stores_worker_list')->where(['mobile' => $this->mobile])->find()) {
            $return['code'] = 0;
            $return['errorMsg'] = '该教练不存在';
            $this->ajaxReturn($return);
        }
        $return = array();
        $filter = array();
        $filter['course_date'] = I('course_date', '');
        $filter['cate_id'] = I('cate_id', 0);
        $filter['coach_id'] = $coach_info['id'];
        $pageNo = I('pageNo', 1);
        $pageSize = I('pageSize', 10);
        $list = D('Course')->getCourseList($pageNo, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => &$val) {
                $val['coach_name'] = M('stores_worker_list')->where(['id' => $val['coach_id'], 'role' => 1])->getField('name');
                $val['classroom_name'] = M('stores_classroom_list')->where(['id' => $val['classroom_id']])->getField('name');
            }
        }
        $return['code'] = 1;
        $return['data'] = $list;
        $return['loadmore'] = (count($list) == $pageSize) ? true : false;
        $this->ajaxReturn($return);
    }

    /*
     * 【教练端】课程详情
     * */
    public function coachCourseInfo()
    {
        if ($this->role != 1) {
            $return['code'] = 0;
            $return['errorMsg'] = '您无权查看';
            $this->ajaxReturn($return);
        }
        if (!$coach_info = M('stores_worker_list')->where(['mobile' => $this->mobile])->find()) {
            $return['code'] = 0;
            $return['errorMsg'] = '该教练不存在';
            $this->ajaxReturn($return);
        }
        $course_id = I('course_id/d', 0);
        $course_info = D('Course')->getCourseInfo($course_id);
        $member_list = M('user_course')->where(['course_id' => $course_id, 'is_deleted' => 0])->select();
        foreach ($member_list as $key => &$val) {
            $user_info = M('user')->find($val['user_id']);
            $val['title'] = $user_info['name'];
            $val['url'] = $user_info['avatarurl'];
            $user_card_info = M('user_card')->alias('a')->join('left join goods b on a.goods_id=b.id')->where(['a.user_id' => $val['user_id']])->field('a.nums, a.freez, b.goods_title')->find();
            $val['nums'] = $user_card_info['nums'];
            $val['freez'] = $user_card_info['freez'];
            $val['cards'] = $user_card_info['goods_title'];
        }
        $return['code'] = 1;
        $return['data'] = [
            'course_data' => $course_info,
            'coach_data' => $coach_info,
            'member_list' => $member_list
        ];
        $this->ajaxReturn($return);
    }

    /**
     * 【教练端】课程签到
     */
    public function courseSignin()
    {
        $course_id = I('course_id/d', 0);
        $member_id = I('member_id/d', 0);
        $user_course_info = M('user_course')->where(['course_id' => $course_id, 'user_id' => $member_id, 'is_deleted' => 0])->find();
        if (!$user_course_info || $user_course_info['status'] != 1) {
            $return['code'] = 0;
            $return['errorMsg'] = '该课程无法签到';
            $this->ajaxReturn($return);
        }
        $course_info = M('course')->where(['id' => $course_id])->find();
        M('user_course')->where(['id' => $user_course_info['id']])->setField('status', 2);
        $data = [
            'freez' => ['exp', 'freez - ' . ($user_course_info['nums'] * $course_info['card_nums'])],
            'nums' => ['exp', 'nums - ' . ($user_course_info['nums'] * $course_info['card_nums'])]
        ];
        M('user_card')->where(['user_id' => $this->user_id])->save($data);
        $return['code'] = 1;
        $return['data'] = 'success';
        $this->ajaxReturn($return);
    }
}